package com.zzgele.qmzb.api.web;

import com.alibaba.boot.dubbo.annotation.DubboConsumer;
import com.zzgele.qmzb.api.core.Result;
import com.zzgele.qmzb.api.core.ResultGenerator;
import com.zzgele.qmzb.api.model.ReportType;
import com.zzgele.qmzb.api.model.VideoReport;
import com.zzgele.qmzb.api.service.ReportTypeService;
import com.zzgele.qmzb.api.service.VideoReportService;
import com.zzgele.qmzb.api.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by Martin on 2017/07/13.
*/
@RestController
@RequestMapping("/video_report")
public class VideoReportController {
    private final Logger logger = LoggerFactory.getLogger(VideoReportController.class);
    @DubboConsumer
    private VideoReportService videoReportService;
    @DubboConsumer
    private ReportTypeService reportTypeService;

    @PostMapping("/create")
    public Result create(Long login_uid, Long video_id, Integer report_type_id) {
        ReportType reportType = reportTypeService.findById(report_type_id);
        if(reportType == null) {
            report_type_id = 0;
        }

        Condition condition = new Condition(VideoReport.class);
        condition.createCriteria().andCondition("uid=" + login_uid + " AND vid=" + video_id);
        List<VideoReport> videoReportList = videoReportService.findByCondition(condition);
        if(videoReportList.size() != 0) {
            return ResultGenerator.genFailResult("提交失败，不能重复举报一个信息");
        }

        VideoReport videoReport = new VideoReport();
        videoReport.setCreateTime(DateUtil.getCurUnixTimeStampInt());
        videoReport.setTypeId(report_type_id);
        videoReport.setUid(login_uid);
        videoReport.setVid(video_id);
        videoReport.setStatus(0);
        videoReport.setUpdateTime(0);
        videoReport.setAdminId(0);
        videoReportService.save(videoReport);

        return ResultGenerator.genSuccessResult("提交成功，感谢您的举报");
    }
}
